﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Kugar.Core.ExtMethod;
using Kugar.UI.Web.AdminLTE.Enums;
using Kugar.UI.Web.AdminLTE.Helpers;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace Kugar.UI.Web.AdminLTE.PageControls.PageForms
{
    public  class PageForm_DatetimePickerField:PageForm_SingleInputFieldBase
    {
        private DateTime? _value = null;
        private DateTime? _minDt = null;
        private DateTime? _maxDt = null;
        private DatetimePickerType _type = DatetimePickerType.Date;
        private string _format = "";
        //private bool _isRequired = false;
        private bool _disabled = false;
        private bool _readonly = false;


        public PageForm_DatetimePickerField(PageForm_Form form, ViewContext viewContext) : base(form, viewContext)
        {
            this.Css("webui-form_datetimepickerfield datetimepicker");
        }

        //public PageForm_DatetimePickerField Require(bool isRequired = true)
        //{
        //    _isRequired = isRequired;

        //    return this;
        //}

        public PageForm_DatetimePickerField Disabled(bool disabled = true)
        {
            _disabled = disabled;

            return this;
        }

        public PageForm_DatetimePickerField ReadOnly(bool @readonly = true)
        {
            _readonly = @readonly;

            return this;
        }

        public PageForm_DatetimePickerField Format(string format)
        {
            _format = format;
            return this;
        }

        public PageForm_DatetimePickerField Value(DateTime? value)
        {
            _value = value;

            return this;
        }

        public PageForm_DatetimePickerField Type(DatetimePickerType type)
        {
            _type = type;

            return this;
        }

        public PageForm_DatetimePickerField ValueLimit(DateTime? minDt, DateTime? maxDt)
        {
            _maxDt = maxDt;
            _minDt = minDt;

            return this;
        }

        protected override void BeforeRender(TextWriter writer)
        {
            if (_format == "")
            {
                _format = Utility.GetDefaultDateFormat(_type);
            }

            this.Data("format", _format)
                .Data("pickertype", _type.ToString())
                .Data("maxdt", _maxDt.ToStringEx(_format))
                .Data("mindt", _minDt.ToStringEx(_format))
                .Data("disabled",_disabled.ToStringEx())
                ;

            if (_value.HasValue)
            {
                if (_minDt.HasValue && _minDt.Value>_value.Value)
                {
                    _value = _minDt.Value;
                }

                if (_maxDt.HasValue && _maxDt.Value<_value.Value)
                {
                    _value = _maxDt.Value;
                }
            }

            base.BeforeRender(writer);

        }

        protected override void RenderContent(TextWriter writer)
        {
            writer.WriteLine($"<input type=\"text\" value=\"{_value.ToStringEx(_format)}\" {_readonly.If("readonly")} {(_isRequired ? "required" : "")} name=\"{_name}\"  class=\"form-control\" />");

            writer.WriteLine($"<span class=\"input-group-btn\"><button type=\"button\" class=\"btn btn-info datetimepickershow\"><i class=\"fa fa-calendar\"></i></button></span>");

        }
    }
}
